/**
 * Created by Dalyn on 2017/5/4.
 */
import React from "react";
import Relay from "react-relay";
import {hashHistory} from 'react-router';
import {Table,message} from 'antd';
import Util from "../../util/Util"
import ReactKey from "../../util/ReactKey"
import InputBox from "../../common/InputBox"
import Pagination from "./Pagination"
import GetProDigestListMutation from "./mutation/GetProDigestListMutation"
import "./css/productionManagement_common.css";
import "./css/productionLicense.css";

import {Button} from 'fly';

export default class ProductionLicense extends React.Component {
    constructor(props){
        super(props);
        this.state={
            proDigestModelList:[
                {flag:false}
            ],
            pageSize:"10",
            pageNum:"1",
            totalResult:"",
            /*客户名*/
            customerName:"",
            styleNo:"",
            enNameError:"",
            searchOrderFlag:true,
            searchCustomerFlag:true,
            searchStyleNoFlag:true,
            loading: true,
            enterpriseId:sessionStorage.getItem("enterpriseId"),
            // roleNumber:sessionStorage.getItem("roleNumber"),
            uid:sessionStorage.getItem("uid"),
        };
        this.style = {
            input:{
                width: "200px",
                height: "28px",
            },
        };
        this.enterpriseId = sessionStorage.getItem("enterpriseId");
        this.roleNumber = sessionStorage.getItem("roleNumber");
        this.operationId = sessionStorage.getItem("uid");
        this.messageFlag = true;
    }
    componentWillMount(){
        if(this.roleNumber == 10001){
            /*业务员*/
            this.proDigestList(this.operationId);
        }else {
            /*其他所有员*/
            this.proDigestList();
        }
    }
    /*查询数据*/
    proDigestList(operationId){
        Relay.Store.commitUpdate(
            new GetProDigestListMutation({
                pageSize: this.state.pageSize,
                pageNum: this.state.pageNum,
                enterpriseId:this.enterpriseId,
                styleNo:this.state.styleNo,
                operationId: operationId?operationId:"",
                customerName:this.state.customerName,
            }),
            {
                onFailure: () => {
                    message.error('网络请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.getProDigestList.responseData.errorCode;
                    let value = response.getProDigestList.responseData.value;
                    if(errorCode=='0'){
                        let proDigestModelList = response.getProDigestList.responseData.proDigestModelList;
                        let totalResult = response.getProDigestList.responseData.totalResult;
                        this.setState({
                            proDigestModelList:proDigestModelList,
                            totalResult:totalResult,
                            loading:false,
                        },()=>{
                            this.refs.pagination.setState({
                                text:this.state.pageNum,
                            })
                        });
                    }else {
                        this.setState({
                            loading:false,
                        });
                        if (errorCode == 500){
                            message.error('服务器错误,请联系系统管理员');
                        }
                    }
                }
            }
        )
    }
    /*搜索*/
    search(){
        let StyleNo = this.refs._searchStyleNo.state.text;
        let textCustomer = this.refs._searchCustomer.state.text;
        // if(textOrder=="" && textCustomer=="")return;
        const {searchOrderFlag,searchCustomerFlag} = this.state;
        if(searchCustomerFlag && searchOrderFlag){
            // console.log("允许搜索");
            if(this.roleNumber == 10001){
                /*业务员*/
                this.setState({
                    pageNum:1,
                },this.proDigestList(this.operationId));
            }else {
                /*其他所有员*/
                this.setState({
                    pageNum:1,
                },this.proDigestList);
            }
        }else {
            this.refs.pagination.setState({
                text:1,
            });
            this.setState({
                proDigestModelList:[],
                loading: false,
                totalResult:0,
            });
            // console.log("不允许搜索");
        }
    }
    /*搜索-订单*/
    searchOrder(){
        let text = this.refs._searchOrder.state.text;
        /*订单号的正则:  /(?!^\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{1,22}/
        * (?!^\d+$)  排除全数字
         (?!^[a-zA-Z]+$) 排除全字母
         [0-9a-zA-Z]{4,23} 字符或字母4-23位，不考虑全为数字和全为字符情况。*/
        let reg = /^[0-9a-zA-Z]*$/;
        if(text && !reg.test(text)){
            this.setState({
                proDigestModelList:[],
                totalResult:0,
                searchOrderFlag:false,
                orderId:"",
            },()=>{
                message.warn("订单号格式不正确")
            });
        }else{
            this.setState({
                enNameError:"",
                searchOrderFlag:true,
                orderId:text
            });
        }
    }
    /*搜索-款号*/
    searchStyleNo(){
        let text = this.refs._searchStyleNo.state.text;
        // let reg = /^[0-9a-zA-Z]*$/;
        let this_=this;
        if(!text){
            this.setState({
                proDigestModelList:[],
                totalResult:0,
                searchStyleNoFlag:false,
                styleNo:"",
            },()=>{
                // Util.messageHint(this_,"warning","款号格式不正确");
            });
        }else{
            this.setState({
                enNameError:"",
                searchOrderFlag:true,
                styleNo:text
            });
        }
    }
    /*搜索-客户*/
    searchCustomer(){
        let text = this.refs._searchCustomer.state.text;
        // console.log(text);
        let reg = /^[a-zA-Z\u4e00-\u9fa5（）]+$/;
        if(text && !reg.test(text)){
            this.setState({
                totalResult:0,
                searchCustomerFlag:false,
                customerName:"",
            },()=>{
                // message.warn("客户名格式不正确")
            });
        }else{
            this.setState({
                enNameError:"",
                searchCustomerFlag:true,
                customerName:text
            });
        }
    }
    /*清空提示信息*/
    clearEnNameError(){
        this.setState({
            enNameError:""
        });
    }
    /*页码*/
    onSure(size,num){
        this.num = num;
        if(this.roleNumber == 10001){
            /*业务员*/
            this.setState({
                loading:true,
                pageSize:size,
                pageNum:num,
            },this.proDigestList(this.operationId))
        }else {
            /*其他所有员*/
            this.setState({
                loading:true,
                pageSize:size,
                pageNum:num,
            },this.proDigestList)
        }
    }

    render() {
        const columns = [
            {
                title: '订单编号',
                dataIndex: 'orderId',
                key: 'orderId',
                render: (text, record) =>{
                    return(
                        <span title={record.orderId}>{record.orderId}</span>
                    )
                }
            },{
                title: '客户',
                dataIndex: 'customerName',
                key: 'customerName',
                render: (text, record) =>{
                    // return(
                    //     <span title={record.partBName}>{record.customerName}</span>
                    // )
                    return (
                        <span title={!record.customerName ? record.traderName : record.customerName} className="">
                            {
                                !record.customerName ?
                                    <span>{record.traderName} {record.traderName != '' && record.traderName != null ? <a style={{color:'red',cursor:'initial'}}>(未关联)</a> : null}</span>
                                    : record.customerName
                            }
                        </span>
                    )
                }
            },{
                title: '款号',
                dataIndex: 'styleNo',
                key: 'styleNo',
                render: (text, record) =>{
                    return(
                        <span>{record.styleNo}</span>
                    )
                }
            },{
                title: '数量',
                dataIndex: 'number',
                key: 'number',
                render: (text, record) =>{
                    return(
                        <span>{record.number}</span>
                    )
                }
            },{
                title: '货期',
                dataIndex: 'finishDate',
                key: 'finishDate',
                render: (text, record) =>{

                    return record.finishDate
                        ?
                        (
                            <span >{Util.dateFormat(record.finishDate)}</span>
                        )
                        :
                        (
                            <span></span>
                        )
                }
            },/*{
                title: '面料进度',
                dataIndex: 'liningPlan',
                key: 'liningPlan',
                render: (text, record) =>{
                    return(
                        <span>{record.liningPlan}</span>
                    )
                }
            },{
                title: '辅料进度',
                dataIndex: 'accessoryPlan',
                key: 'accessoryPlan',
                render: (text, record) =>{
                    return(
                        <span>{record.accessoryPlan}</span>
                    )
                }
            },*/{
                title: '操作',
                dataIndex: 'operate',
                key: 'operate',
                onCellClick:(record, e)=>{
                    let target = e.target;
                    if(target.id == "edit"){
                        hashHistory.push({pathname:'/productionlicenseindex/',state:{'pdId': record.pdId,editLook:true }})
                    }else if(target.id == "look"){
                        hashHistory.push({pathname:'/productionlicenseindex/',state:{'pdId': record.pdId,editLook:false }})
                    }else {
                        return
                    }
                },
                render: (text, record) =>{
                    return record.operationId != this.state.uid?
                        (
                            <span className="production_management_tb_tbha" id = "look">查看</span>
                        )
                        :
                        (
                            <span className="">
                                <span className="production_management_tb_tbha" id = "look">查看</span>
                                <span className="production_management_tb_tbha" id = "edit">编辑</span>
                            </span>
                        )
                }
            }
        ];
        const proDigestModelList = ( null == this.state.proDigestModelList || this.state.proDigestModelList.length == 0)?[]:this.state.proDigestModelList;
        return (
            <div className="poduction_manage_box">
                <div className="production_management_set_search clear">
                    {/*<div className="production_management_set_searchInp">
                        <span className="production_management_set_searchInp_sp">订单：</span>
                        <InputBox className="production_management_set_searchInp_inp pro_overflow_inputSty " space={20} ref={"_searchOrder"} style={this.style.input} defaultvalute="请输入订单号" changeon={this.searchOrder.bind(this,"searchOrder")}
                        />
                    </div>*/}
                    <div className="production_management_set_searchInp">
                        <span className="production_management_set_searchInp_sp">款号：</span>
                        <InputBox className="production_management_set_searchInp_inp pro_overflow_inputSty " space={20} ref={"_searchStyleNo"} style={this.style.input} defaultvalute="请输入款号" changeon={this.searchStyleNo.bind(this,"searchStyleNo")}
                                  />
                    </div>
                    <div className="production_management_set_searchInp">
                        <span className="production_management_set_searchInp_sp">客户：</span>
                        <InputBox className="production_management_set_searchInp_inp pro_overflow_inputSty " space={20} ref={"_searchCustomer"} style={this.style.input} defaultvalute="请输入客户名称" maxlength={40} changeon={this.searchCustomer.bind(this,"searchCustomer")}
                        />
                    </div>
                    <div className="production_management_set_searchBtn">
                        <Button onClick={this.search.bind(this)}>
                           搜索
                        </Button>

                        <span className="production_management_set_searchHint">{this.state.enNameError}</span>
                    </div>
                </div>
                <Table
                    columns={columns}
                    dataSource={proDigestModelList}
                    pagination={false}
                    rowKey={record => record.pdId+record.orderId}
                    style={{width:'100%'}}
                    className="order_table"
                    loading={this.state.loading}
                />
                <Pagination eachSize={this.state.pageSize} dataSize={this.state.totalResult} onSure={this.onSure.bind(this)} ref={'pagination'}/>
            </div>
        )
    }
}
